<?php

/**
 * Simple pagination
 * There is a small example of usage bellow:
 *
 *   $pagination = new AttitudePagination($current_page, $total_pages);
 *
 *   <?php if($pagination->getPrev()): ?>
 *     <a href="?page=<?php echo $pagination->getPrev() ?>">Previous</a>
 *   <?php endif; ?>
 *   <?php foreach($pagination->getPages() as $page): ?>
 *     <?php if($page == '...'): continue; ?>
 *     <?php if($page == $pagination->getCurrent()): ?>
 *       <strong> <?php echo $page ?> </strong>
 *     <?php else: ?>
 *       <a href="?page=<?php echo $page ?>"><?php echo $page ?></a>
 *     <?php endif; ?>
 *   <?php if($pagination->getNext()): ?>
 *     <a href="?page=<?php echo $pagination->getNext() ?>">Next</a>
 *   <?php endif; ?>
 *
 * @author Apple
 */

class AttitudePagination
{
	protected
		$_total,
		$_current,
		$_next,
		$_prev,
		$_pages = array();
	
	/**
	 * Contructor
	 * @param  unsigned int $current
	 * @param  unsigned int $total
	 * @return AttitudePagination
	 */
	public function __construct($current, $total)
	{
		if(is_numeric($current) && is_numeric($total)) {
			if($current >= 1 && $current <= $total) {
				$this->_current = $current;
				$this->_total   = $total;
				$this->_next    = $current + 1;
				$this->_prev    = $current - 1;
			} else throw new Exception('Current and/or total page out of range');
		} else throw new Exception('Invalid arguments were specified');
	}
	
	
	/**
	 * Get plain array of pages (e.g. array(1, '...', 4, 5, 6))
	 * It is the main method of this class that you should class
	 *
	 * @return array
	 */
	public function getPages()
	{
		if(!$this->_pages) {
			return $this->buildPages();
		} else return $this->_pages;
	}
	
	/**
	 * The main algorithm
	 * It is used to build array of pages and dots without processing
	 * them into HTML links, just a plain array
	 * 
	 * @return array
	 */
	protected function buildPages()
	{
		if($this->_total > 5) {
			if($this->_current >= 4) {
				$this->_pages[] = 1;
				$this->_pages[] = '...';
				$this->_pages[] = $this->_prev;
				$this->_pages[] = $this->_current;
				
				if($this->_current != $this->_total && $this->_next != $this->_total && $this->_next+1 != $this->_total) {
					$this->_pages[] = $this->_next;
					$this->_pages[] = '...';
					$this->_pages[] = $this->_total;
				} else {
					if($this->_current == $this->_total) {
						// Nothing to do
					} elseif($this->_next == $this->_total) {
						$this->_pages[] = $this->_next;
					} elseif($this->_next+1 == $this->_total) {
						$this->_pages[] = $this->_next;
						$this->_pages[] = $this->_next + 1;
					}
				}
			} else { 
				for($j = 1; $j <= 4; $j++) {
					$this->_pages[] = $j;
				}
				$this->_pages[] = '...';
				$this->_pages[] = $this->_total;
			}
		} else {
			for($j = 1; $j <= $this->_total; $j++) {
				$this->_pages[] = $j;
			}
		}
		
		return $this->_pages;
	}
	
	
	/**
	 * Get the current page
	 * @return int
	 */
	public function getCurrent()
	{
		if($this->_current) {
			return $this->_current;
		} else throw new Exception('Current page has not been set');
	}
	
	
	/**
	 * Get the next page if is
	 * Use this method instead of checking manually
	 * @return int
	 */
	public function getNext()
	{
		if($this->getCurrent() != $this->_total) {
			return $this->_next;
		} else return false;
	}
	
	
	/**
	 * Get the previous page if is
	 * Use this method instead of checking manually
	 * @return int
	 */
	public function getPrev()
	{
		if($this->getCurrent() > 1) {
			return $this->_prev;
		} else return false;
	}
};
